<?php
require_once 'DBUtil.class.php';
require_once 'Message.class.php';
require_once 'Reply.class.php';
require_once 'ReplyAction.class.php';
class MessageAction {
	//添加
	function add($msg) {
		$con = DBUtil::getCon();
		$sql = "INSERT INTO message (name, title, content, create_time) VALUES ('{$msg->name}', '{$msg->title}', '" . addslashes($msg -> content) . "',now())";
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return mysql_insert_id();
		}
	}

	//根据ID查询
	function queryById($msg) {
		$con = DBUtil::getCon();
		$sql = "SELECT * FROM message WHERE id=" . $msg -> id;
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$msg -> name = $row['name'];
		$msg -> title = $row['title'];
		$msg -> content = $row['content'];
		$msg -> create_time = $row['create_time'];
		return $msg;
	}

	//查询所有
	function queryAll() {
		$con = DBUtil::getCon();
		$sql = "SELECT * FROM message";
		$result = mysql_query($sql);
		$msgs = array();
		if ($result) {
			while ($row = mysql_fetch_array($result)) {
				$msg = new Message();
				$msg -> id = $row['id'];
				$msg -> name = $row['name'];
				$msg -> title = $row['title'];
				$msg -> content = $row['content'];
				$msg -> create_time = $row['create_time'];
				$msgs[] = $msg;
			}
		}
		return $msgs;
	}

	//修改
	function save($msg) {
		$con = DBUtil::getCon();
		$sql = "UPDATE message SET name='{$msg->name}', title={$msg->title}, content ='{$msg->content}' WHERE id={$user->id}";
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return true;
		}
	}

	//删除
	function delete($msg) {
		$con = DBUtil::getCon();
		$replyAction = new ReplyAction();
		$reply =$replyAction -> queryByMsgId($msg);
		if($reply){
			$replyAction -> delete($msg);
		}
		$sql = "DELETE FROM message WHERE id=" . $msg -> id;
		if (!mysql_query($sql)) {
			die('Error: ' . mysql_error());
		} else {
			return true;
		}
	}

}
?>